import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/Xwidgets/line.dart';
import 'package:yp/config/config.dart';
import 'package:yp/config/event_bus.dart'; // import 'dart:js' as js;
import 'package:yp/helper.dart';
import 'package:yp/pages/order_settlement/widgets/CachedImage.dart';
import 'package:yp/pages/order_settlement/widgets/style.dart';
// import 'package:yp/pages/order_settlements/state.dart';
import 'package:yp/pages/order_settlement/widgets/text_input.dart';
import 'package:yp/pages/order_settlements/logic.dart';
import 'package:yp/router.dart';
import 'package:yp/services/api.dart';
import 'package:yp/utils/util.dart';
import 'package:yp/widgets/button_widget.dart';

import '../../helper.dart';
import 'logic.dart';

import 'package:yp/pages/address_selector/address_model.dart';
///普通订单结算
typedef ClickCallback = void Function(int selectIndex, String selectString,
    {Map<String, dynamic> info});

var logic;

var state;

var arguments;

class OrderSettlementPage extends StatefulWidget {
  @override
  _OrderSettlementPageState createState() => _OrderSettlementPageState();
}

class _OrderSettlementPageState extends State<OrderSettlementPage>
    with SingleTickerProviderStateMixin {
  @override
  void initState() {
    arguments = Get.arguments;
    getagm();
    super.initState();
  }

  Function setGift(index) {
    logic.clickGift(index);
  }

  @override
  Widget build(
    BuildContext context,
  ) {
    return WillPopScope(
      onWillPop: () async {
        Get.back(result: true);
        return true;
      },
      child: Scaffold(
          appBar: AppBar(
            backgroundColor: Config.mainColor,
            brightness: Brightness.light,
            iconTheme: IconThemeData(color: Colors.white),
            title: Text(
              "结算",
              style: TextStyle(color: Colors.white, fontSize: 32.sp),
            ),
            centerTitle: true,
            elevation: 0,
          ),
          body: GetBuilders(Mainbody)),
    );
  }

  Widget Mainbody() {
    print(">>>>>@@@>>>>>>>>>${state.total}");
    return state.total != null
        ? Column(
            children: [
              Expanded(
                child: ListView(shrinkWrap: true, primary: true, children: [
                  Column(
                    children: [
                      Container(
                        padding:
                            EdgeInsets.only(left: 30.w, right: 30.w, top: 20.w),
                        decoration: BoxDecoration(
                          color: Colors.white,
                        ),
                        child: Column(
                          children: [
                            Column(children: [
                              state.orderAddress != null &&
                                      state.orderAddress["cont_empe"] != null
                                  ? GestureDetector(
                                      onTap: () {
                                        if (!state.isZiTi) {
                                          Get.toNamed(RouteConfig.addressList,
                                              arguments: {
                                                "userNum": state.userNum,
                                                'adr_num_id': state
                                                    .orderAddress['adr_num_id']
                                              }).then((value) {
                                            if (value["address"] != null) {
                                              print('aaaaaaaaaaaaaaaaaaa');
                                              print(value["address"]);
                                              changeSelectAddress(
                                                  value["address"]);
                                              state.orderAddress =
                                                  value["address"];
                                              state.disable = false;
                                              logic.onReady();
                                            }
                                          });
                                        } else {
                                          toast("请关闭自提");
                                        }
                                      },
                                      child: InkWell(
                                          child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        crossAxisAlignment:
                                            CrossAxisAlignment.center,
                                        children: [
                                          Container(
                                            width: 650.w,
                                            child: Column(
                                              mainAxisAlignment:
                                                  MainAxisAlignment.start,
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.start,
                                              children: [
                                                RichText(
                                                  text: TextSpan(
                                                    children: [
                                                      TextSpan(
                                                        text: state
                                                            .orderAddress[
                                                                "cont_empe"]
                                                            .toString(),
                                                        style: TextStyle(
                                                            color: Colors.black,
                                                            fontSize: 28.w),
                                                      ),
                                                      WidgetSpan(
                                                          child: SizedBox(
                                                        width: 20.w,
                                                      )),
                                                      TextSpan(
                                                        text: state
                                                            .orderAddress[
                                                                "contact_no"]
                                                            .toString(),
                                                        style: TextStyle(
                                                            color: Colors.black,
                                                            fontSize: 28.sp),
                                                      ),
                                                      state.orderAddress[
                                                                  "default_sign"] ==
                                                              1
                                                          ? WidgetSpan(
                                                              child: Container(
                                                                  margin: EdgeInsets.only(
                                                                      left:
                                                                          10.w),
                                                                  padding: EdgeInsets.symmetric(
                                                                      horizontal:
                                                                          5.w,
                                                                      vertical:
                                                                          2.w),
                                                                  color: Config
                                                                      .mainColor,
                                                                  child: Text(
                                                                    "默认",
                                                                    style:
                                                                        TextStyle(
                                                                      fontSize:
                                                                          20.sp,
                                                                      color: Colors
                                                                          .white,
                                                                    ),
                                                                  )),
                                                            )
                                                          : WidgetSpan(
                                                              child:
                                                                  Container()),
                                                    ],
                                                  ),
                                                ),
                                                SizedBox(
                                                  height: 10.w,
                                                ),
                                                Text(
                                                  "${state.orderAddress["prv_name"]} ${state.orderAddress["city_name"]} ${state.orderAddress["city_area_name"]} ${state.orderAddress["town_name"] ?? ''} ${state.orderAddress["adr"]}",
                                                  style: TextStyle(
                                                    color: Colors.black,
                                                    fontSize: 26.sp,
                                                  ),
                                                  maxLines: 100,
                                                  textAlign: TextAlign.start,
                                                  overflow:
                                                      TextOverflow.ellipsis,
                                                ),
                                                SizedBox(
                                                  height: 5.w,
                                                ),
                                                state.isZiTi
                                                    ? Text(
                                                        "您已经选择自提，商品到达自提点后凭借提货码提货",
                                                        style: TextStyle(
                                                          color:
                                                              Color(0xffDB2132),
                                                          fontSize: 22.sp,
                                                        ),
                                                        maxLines: 2,
                                                        textAlign:
                                                            TextAlign.start,
                                                        overflow: TextOverflow
                                                            .ellipsis,
                                                      )
                                                    : Container(),
                                              ],
                                            ),
                                          ),
                                          SizedBox(
                                            width: 5.w,
                                          ),
                                          Center(
                                            child: Icon(
                                              Icons.arrow_forward_ios_rounded,
                                              size: 15,
                                              color: Color(0xffB7B7B7),
                                            ),
                                          ),
                                        ],
                                      )),
                                    )
                                  : GestureDetector(
                                      onTap: () {
                                        Get.toNamed(RouteConfig.addressList,
                                            arguments: {
                                              "userNum": state.userNum,
                                              'adr_num_id':
                                                  state.orderAddress != null
                                                      ? state.orderAddress[
                                                          'adr_num_id']
                                                      : null
                                            }).then((value) {
                                          print("asd$value");
                                          if (value["address"] != null) {
                                            state.orderAddress =
                                                value["address"];
                                            state.disable = false;
                                            state.isZiTi = false;
                                            logic.onReady();
                                          }
                                        });
                                      },
                                      child: Container(
                                        height: 50.w,
                                        child: Text("请选择地址"),
                                      )),
                              Container(
                                padding: EdgeInsets.only(top: 9.w, bottom: 9.w),
                                child: Divider(
                                  color: Color(0xffcccccc),
                                ),
                              ),
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Text(
                                    "您可以选择附近的自提点去提货",
                                    style: TextStyle(
                                      color: Color(0xff666666),
                                      fontSize: 24.sp,
                                    ),
                                  ),
                                  Container(
                                      width: 240.w,
                                      height: 60.w,
                                      child: FittedBox(
                                        fit: BoxFit.scaleDown,
                                        alignment: Alignment.centerRight,
                                        child: CupertinoSwitch(
                                          value: state.isZiTi,
                                          activeColor: Config.mainColor,
                                          onChanged: (v) {
                                            if (!state.isZiTi) {
                                              if (logic.panduan()) {
                                                logic.selfHelpAddress();
                                                showDialog(
                                                  context: context,
                                                  builder: (context) {
                                                    return _selectProduct(
                                                        context);
                                                  },
                                                );
                                              }
                                            }
                                            if (state.isZiTi) {
                                              setState(() {
                                                state.isZiTi = false;
                                              });
                                              logic.selectSwitch1();
                                            }
                                            // if(！state.isZiTi)

                                            // if (state.type != 3) {
                                            //   state.isZiTi =
                                            //       !state.isZiTi;
                                            //   print(state.isZiTi);
                                            //   if (!state.isZiTi) {
                                            //     logic
                                            //         .selectSwitch1();
                                            //   }
                                            // }
                                            // logic.update();
                                            // if (state.isZiTi &&
                                            //     state.type != 3) {
                                            //   if(logic.panduan()){
                                            //        showDialog(
                                            //     context: context,
                                            //     builder: (context) {
                                            //       return _selectProduct(
                                            //           context);
                                            //     },
                                            //   );
                                            //   }
                                            //
                                            // }
                                          },
                                        ),
                                      )),
                                ],
                              ),
                              state.isZiTi
                                  ? Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment:
                                          CrossAxisAlignment.center,
                                      children: [
                                        Container(
                                          width: 580.w,
                                          child: Text(
                                            state.adr,
                                            style: TextStyle(
                                              color: Color(0xff333333),
                                              fontSize: 26.sp,
                                            ),
                                            maxLines: 2,
                                          ),
                                        ),
                                        Center(
                                          child: GestureDetector(
                                            onTap: () {
                                              // Get.toNamed(
                                              //     RouteConfig
                                              //         .addressList);

                                              showDialog(
                                                context: context,
                                                builder: (context) {
                                                  return _selectProduct(
                                                      context);
                                                },
                                              );
                                            },
                                            child: Container(
                                              width: 96.w,
                                              height: 42.w,
                                              decoration: BoxDecoration(
                                                borderRadius: BorderRadius.all(
                                                    Radius.circular(100)),
                                                border: Border.all(
                                                  color: Color(0xffe1e1e1),
                                                ),
                                              ),
                                              child: Center(
                                                child: Text(
                                                  "修改",
                                                  style: TextStyle(
                                                    fontSize: 24.sp,
                                                    color: Color(0xff666666),
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                        )
                                      ],
                                    )
                                  : Container(),
                              SizedBox(
                                height: 20.w,
                              ),
                            ]),
                          ],
                        ),
                      ),
                      Container(
                        height: 24.w,
                      ),
                      //商品部分
                      Container(
                        child: Column(
                          children: [
                            ListView.builder(
                              itemBuilder: (ctx, index) {
                                return Visibility(
                                    visible: (state.goodsItem[index]
                                                    ["ec_packages"] !=
                                                null &&
                                            state
                                                    .goodsItem[index]
                                                        ["ec_packages"]
                                                    .length >
                                                0) ||
                                        (state.goodsItem[index]
                                                    ["stock_out_list"] !=
                                                null &&
                                            state
                                                    .goodsItem[index]
                                                        ["stock_out_list"]
                                                    .length >
                                                0),
                                    child: Container(
                                        color: Colors.white,
                                        padding: EdgeInsets.only(
                                            left: 20.w, right: 20.w),
                                        margin: EdgeInsets.only(bottom: 24.w),
                                        child: Column(
                                          crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                          children: [
                                            Container(
                                              padding: EdgeInsets.only(
                                                  top: 20.w, bottom: 20.w),
                                              decoration: BoxDecoration(
                                                border: Border(
                                                  bottom: BorderSide(
                                                    color: Color(0xfff0f0f0),
                                                  ),
                                                ),
                                              ),
                                              child: Row(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.center,
                                                mainAxisAlignment:
                                                    MainAxisAlignment.start,
                                                children: [
                                                  Image(
                                                    image: AssetImage(
                                                        "assets/images/orderStore.png"),
                                                    width: 36.w,
                                                  ),
                                                  SizedBox(
                                                    width: 5.w,
                                                  ),
                                                  Text(
                                                    logic.state.goodsItem[index]
                                                        ["ec_shop_name"],
                                                    style: TextStyle(
                                                        fontSize: 26.sp,
                                                        color:
                                                            Color(0xff333333)),
                                                  ),
                                                ],
                                              ),
                                            ),

                                            //在配送范围
                                            _inDeliveryView(context, index),
                                            //不在配送范围提示
                                            _notInDeliveryTipView(
                                                context, index),
                                            // 不在配送范围商品
                                            _notInDeliveryView(context, index),
                                          ],
                                        )));
                              },
                              shrinkWrap: true,
                              physics: NeverScrollableScrollPhysics(),
                              itemCount: logic.state.goodsItem != null &&
                                      logic.state.goodsItem.length > 0
                                  ? logic.state.goodsItem.length
                                  : 0,
                            ),
                          ],
                        ),
                      ),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          state.settData['integral'] != null &&
                                  state.settData['integral']['usr_point'] > 0
                              ? Container(
                                  padding: EdgeInsets.symmetric(
                                    vertical: 20.w,
                                    horizontal: 20.w,
                                  ),
                                  color: Colors.white,
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        children: [
                                          Text(
                                            // state.couponName == '' &&
                                            state.isJiFen == true
                                                ? '使用${state.total["usr_point"] == null ? '0' : state.total["usr_point"].toString()}积分抵扣￥${state.total["integral_discount"].toString() ?? '0'}'
                                                : "是否使用积分支付",
                                            style: TextStyle(
                                              color: Color(0xff666666),
                                              fontSize: 28.sp,
                                            ),
                                          ),
                                          SizedBox(
                                            height: 10.w,
                                          ),
                                          Text(
                                            "使用积分抵扣无法使用福利卡",
                                            style: TextStyle(
                                              color: Colors.grey[400],
                                              fontSize: 19.sp,
                                            ),
                                          ),
                                        ],
                                      ),
                                      Container(
                                          width: 240.w,
                                          height: 60.w,
                                          child: FittedBox(
                                            fit: BoxFit.scaleDown,
                                            alignment: Alignment.centerRight,
                                            child: CupertinoSwitch(
                                              value: state.total["usr_point"] ==
                                                      null
                                                  ? false
                                                  : state.isJiFen,
                                              activeColor: Config.mainColor,
                                              onChanged: (v) {
                                                setState(() {
                                                  // if (state.couponName == '') {
                                                  state.GiftcardList.forEach(
                                                      (element) {
                                                    element['click'] = false;
                                                  });
                                                  state.isGiftNum = 0;
                                                  logic.selectSwitch();
                                                  // }
                                                });
                                              },
                                            ),
                                          )),
                                    ],
                                  ),
                                )
                              : SizedBox(),
                          // 配送类型
                          TextInputWidget(
                            title: "配送类型",
                            suffixWidget: GestureDetector(
                              onTap: () {
                                if (state.type != 3) {
                                  _showCupertinoActionSheet1(context);
                                }
                              },
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                  Text(
                                    state.deliveryType == 0
                                        ? "立即发货"
                                        : state.deliveryType == 1
                                            ? "预约发货"
                                            : "暂不发货",
                                    style: TextStyle(
                                      fontSize: 28.sp,
                                      color: Color(0xff333333),
                                    ),
                                  ),
                                  Icon(
                                    Icons.arrow_forward_ios_rounded,
                                    color: Color(0xffcccccc),
                                    size: 14,
                                  )
                                ],
                              ),
                            ),
                          ),
                          // 发货日期
                          state.deliveryType == 1
                              ? TextInputWidget(
                                  title: "发货日期",
                                  suffixWidget: GestureDetector(
                                    onTap: () {
                                      DatePicker.showDatePicker(context,
                                          showTitleActions: true,
                                          minTime: DateTime(2010, 1, 1),
                                          maxTime: DateTime(2022, 12, 31),
                                          onConfirm: (date) {
                                        state.dateTime = date;
                                        logic.update();
                                      },
                                          currentTime: DateTime.parse(
                                              DateFormat("yyyy-MM-dd")
                                                  .format(state.dateTime)),
                                          locale: LocaleType.zh);
                                    },
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.end,
                                      children: [
                                        Text(
                                          "${DateFormat("yyyy-MM-dd").format(state.dateTime)}",
                                          style: TextStyle(
                                            fontSize: 28.sp,
                                            color: Color(0xff222222),
                                          ),
                                        ),
                                        Icon(
                                          Icons.arrow_forward_ios_rounded,
                                          color: Color(0xffcccccc),
                                          size: 14,
                                        )
                                      ],
                                    ),
                                  ),
                                )
                              : Container(),
                          // 优惠券
                          TextInputWidget(
                            title: "优惠券",
                            suffixWidget: InkWell(
                              onTap: () {
                                selectProduct(context);
                                // if (state.couponList.length > 0) {
                                //   selectProduct(context);
                                // } else {
                                //   toast('暂无可用优惠券');
                                // }
                              },
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                  state.couponName == ''
                                      ? ConstrainedBox(
                                          constraints: BoxConstraints(
                                            maxWidth: 250.w,
                                          ),
                                          child: Text(
                                            state.couponList.length > 0
                                                ? "请选择优惠券"
                                                : '暂无可用券',
                                            style: TextStyle(
                                              fontSize: 28.sp,
                                              color: Color(0xffcccccc),
                                            ),
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        )
                                      : ConstrainedBox(
                                          constraints: BoxConstraints(
                                            maxWidth: 250.w,
                                          ),
                                          child: Text(
                                            state.couponName,
                                            style: TextStyle(
                                              fontSize: 28.sp,
                                              color: Color(0xff000000),
                                            ),
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                          ),
                                        ),
                                  Icon(
                                    Icons.arrow_forward_ios_rounded,
                                    color: Color(0xffcccccc),
                                    size: 14,
                                  )
                                ],
                              ),
                            ),
                          ),
                          if (state.type != 3 &&
                              state.total["all_payment"].toString() != '0.0')
                            // Container(
                            //   color: Colors.white,
                            //   padding: EdgeInsets.symmetric(
                            //     vertical: 20.w,
                            //     horizontal: 20.w,
                            //   ),
                            //   child: Column(
                            //     crossAxisAlignment:
                            //         CrossAxisAlignment.start,
                            //     children: [
                            // Text(
                            //   '支付方式',
                            //   style: TextStyle(
                            //     fontSize: 28.sp,
                            //     color: Color(0xff666666),
                            //   ),
                            // ),
                            // SizedBox(
                            //   height: 15.w,
                            // ),
                            // Row(
                            //   mainAxisAlignment:
                            //       MainAxisAlignment.start,
                            //   children: [
                            //     SelectButton(
                            //       title: "全款支付",
                            //       isSelected:
                            //           state.paytype == 1,
                            //       onTap: () =>
                            //           logic.setPayType(1),
                            //     ),
                            //     SizedBox(width: 20.w),
                            // SelectButton(
                            //   title: "订金",
                            //   isSelected:
                            //       state.paytype == 2,
                            //   onTap: () =>
                            //       logic.setPayType(2),
                            // ),
                            // SizedBox(width: 20.w),
                            // SelectButton(
                            //   title: "尾款",
                            //   isSelected: state.paytype == 3,
                            //   onTap: () => logic.setPayType(3),
                            // ),
                            //   ],
                            // )
                            //     ],
                            //   ),
                            // ),

                            /// 订金金额
                            if (state.paytype == 2 &&
                                state.total["all_payment"].toString() != '0.0')
                              Container(
                                color: Colors.white,
                                padding: EdgeInsets.symmetric(horizontal: 20.w),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Row(
                                      children: [
                                        // Text(
                                        //   '订金金额',
                                        //   style: TextStyle(
                                        //     color:
                                        //         Color(0xff666666),
                                        //     fontSize: 28.w,
                                        //   ),
                                        // ),
                                        //   SizedBox(
                                        //     width: 30.w,
                                        //   ),
                                        //   Container(
                                        //     width: 550.w,
                                        //     child: TextField(
                                        //       controller:
                                        //           state.controller1,
                                        //       textAlign:
                                        //           TextAlign.end,
                                        //       keyboardType: TextInputType
                                        //           .numberWithOptions(
                                        //               decimal: true),
                                        //       inputFormatters: [
                                        //         BlacklistingTextInputFormatter(
                                        //             new RegExp(
                                        //                 '[\\-|\\ ]'))
                                        //       ],
                                        //       onChanged: (value) {
                                        //         logic.countMoney(
                                        //             value);
                                        //         if (value == '') {
                                        //           state.startMoney =
                                        //               null;
                                        //           state.endMoney =
                                        //               null;
                                        //           logic.update();
                                        //         }
                                        //       },
                                        //       decoration:
                                        //           InputDecoration(
                                        //         isDense: true,
                                        //         border:
                                        //             InputBorder.none,
                                        //         hintText:
                                        //             '请填写大于0的数字金额',
                                        //         hintStyle: TextStyle(
                                        //           fontSize: 28.sp,
                                        //           color: Color(
                                        //               0xffcccccc),
                                        //         ),
                                        //       ),
                                        //     ),
                                        //   ),
                                      ],
                                    ),
                                    SizedBox(
                                      width: 750.w,
                                      height: 1,
                                      child: DecoratedBox(
                                        decoration: BoxDecoration(
                                            color: Color(0xffefefef)),
                                      ),
                                    ),
                                  ],
                                ),
                              ),

                          /// 尾款金额
                          if (state.paytype == 2 &&
                              state.total["all_payment"].toString() != '0.0')
                            Container(
                              color: Colors.white,
                              padding: EdgeInsets.symmetric(
                                  vertical: 20.w, horizontal: 20.w),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                        '尾款金额',
                                        style: TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: 28.w,
                                        ),
                                      ),
                                      SizedBox(
                                        width: 30.w,
                                      ),
                                      Row(
                                        children: [
                                          Text(
                                            '${state.endMoney != null ? state.endMoney : '自动带出'}',
                                            style: TextStyle(
                                              fontSize: 28.sp,
                                              color: state.endMoney != null
                                                  ? Color(0xff000000)
                                                  : Color(0xffcccccc),
                                            ),
                                          ),
                                          if (state.endMoney != null)
                                            Text(
                                              '元',
                                              style: TextStyle(
                                                fontSize: 28.sp,
                                                color: state.endMoney != null
                                                    ? Color(0xff000000)
                                                    : Color(0xffcccccc),
                                              ),
                                            )
                                        ],
                                      ),
                                    ],
                                  ),
                                  SizedBox(
                                    height: 15.w,
                                  ),
                                  SizedBox(
                                    width: 750.w,
                                    height: 1,
                                    child: DecoratedBox(
                                      decoration: BoxDecoration(
                                          color: Color(0xffefefef)),
                                    ),
                                  ),
                                ],
                              ),
                            ),

                          /// 尾款预计付款日期
                          if (state.paytype == 2 &&
                              state.total["all_payment"].toString() != '0.0')
                            InkWell(
                              onTap: () {
                                DatePicker.showDatePicker(context,
                                    showTitleActions: true,
                                    minTime: DateTime(2010, 1, 1),
                                    maxTime: DateTime(2022, 12, 31),
                                    onConfirm: (date) {
                                  logic.selectDate(date);
                                },
                                    currentTime: DateTime.now(),
                                    locale: LocaleType.zh);
                              },
                              child: Container(
                                color: Colors.white,
                                padding: EdgeInsets.only(
                                  left: 20.w,
                                  right: 20.w,
                                  bottom: 20.w,
                                ),
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  children: [
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Text(
                                          '尾款预计付款日期',
                                          style: TextStyle(
                                            color: Color(0xff666666),
                                            fontSize: 28.w,
                                          ),
                                        ),
                                        SizedBox(
                                          width: 30.w,
                                        ),
                                        Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.end,
                                          children: [
                                            Text(
                                              '${state.time1 != '' ? state.time1 : '请选择日期'}',
                                              style: TextStyle(
                                                fontSize: 28.sp,
                                                color: state.time1 != ''
                                                    ? Color(0xff000000)
                                                    : Color(0xffcccccc),
                                              ),
                                            ),
                                            Icon(
                                              Icons.arrow_forward_ios_rounded,
                                              color: Color(0xffcccccc),
                                              size: 14,
                                            )
                                          ],
                                        )
                                      ],
                                    ),
                                    SizedBox(
                                      height: 15.w,
                                    ),
                                    SizedBox(
                                      width: 750.w,
                                      height: 1,
                                      child: DecoratedBox(
                                        decoration: BoxDecoration(
                                            color: Color(0xffefefef)),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          // 增值税发票，全部显示
                          TextInputWidget(
                            title: "是否需要发票",
                            suffixWidget: InkWell(
                              onTap: () {
                                Get.toNamed(RouteConfig.drawBillPage,
                                    arguments: {
                                      'tml_num_id': "",
                                      'billHeader': state.billHeader,
                                      'isNeedBill': state.isNeedBill != "不需要",
                                      'p': state.p,
                                    }).then((value) {
                                  if (value != null) {
                                    print("Get.arguments=${value}");
                                    state.isNeedBill = value['a'];
                                    state.billHeader = value['b'];
                                    if (value['c'] != null) {
                                      state.p = value['c'];
                                      eventBus.fire(InvoiceEvent(value['c']));
                                    }
                                    logic.update();
                                    setState(() {});
                                  }
                                });
                                // selectProduct(context);
                                // if (state.couponList.length > 0) {
                                //   selectProduct(context);
                                // } else {
                                //   toast('暂无可用优惠券');
                                // }
                              },
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: [
                                  ConstrainedBox(
                                    constraints: BoxConstraints(
                                      maxWidth: 250.w,
                                    ),
                                    child: Text(
                                      '${state.billHeader}${state.isNeedBill}',
                                      style: TextStyle(
                                        fontSize: 28.sp,
                                        color: Color(0xffcccccc),
                                      ),
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                    ),
                                  ),
                                  Icon(
                                    Icons.arrow_forward_ios_rounded,
                                    color: Color(0xffcccccc),
                                    size: 14,
                                  )
                                ],
                              ),
                            ),
                          ),

                          // 订单备注
                          Container(
                            color: Colors.white,
                            child: Padding(
                              padding: EdgeInsets.symmetric(
                                horizontal: 20.w,
                              ),
                              child: Container(
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.all(
                                    Radius.circular(20.w),
                                  ),
                                  border: Border.all(
                                    color: Color(0xfff0f0f0),
                                  ),
                                ),
                                padding: EdgeInsets.symmetric(horizontal: 20.w),
                                child: TextField(
                                  controller: state.controller,
                                  style: TextStyle(fontSize: 24.sp),
                                  maxLines: 5,
                                  inputFormatters: <TextInputFormatter>[
                                    LengthLimitingTextInputFormatter(30) //限制长度
                                  ],
                                  decoration: InputDecoration(
                                    focusedBorder: InputBorder.none,
                                    enabledBorder: InputBorder.none,
                                    hintText: '添加订单备注',
                                    hintStyle: TextStyle(fontSize: 24.sp),
                                  ),
                                ),
                              ),
                            ),
                          ),
                          Container(
                            color: Colors.white,
                            height: 24.w,
                          ),
                        ],
                      ),
                      Container(
                        height: 24.w,
                      ),
                      Container(
                        color: Colors.white,
                        padding: EdgeInsets.all(20.w),
                        child: Column(
                          children: [
                            Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  "商品金额",
                                  style: TextStyle(
                                    color: Color(0xff666666),
                                    fontSize: 28.sp,
                                  ),
                                ),
                                Text(
                                  state.total["all_fee"].toString(),
                                  style: TextStyle(
                                    color: Color(0xff222222),
                                    fontSize: 28.sp,
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(
                              height: 20.w,
                            ),
                            Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  "其他优惠",
                                  style: TextStyle(
                                    color: Color(0xff666666),
                                    fontSize: 28.sp,
                                  ),
                                ),
                                Text(
                                  state.total["promotion_total_discount"]
                                      .toString(),
                                  style: TextStyle(
                                    color: Color(0xff222222),
                                    fontSize: 28.sp,
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(
                              height: 20.w,
                            ),
                            Row(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  "优惠券抵扣",
                                  style: TextStyle(
                                    color: Color(0xff666666),
                                    fontSize: 28.sp,
                                  ),
                                ),
                                Text(
                                  state.iscou == true
                                      ? state.total["coupon_discount"]
                                          .toString()
                                      : '0.00',
                                  style: TextStyle(
                                    color: Color(0xff222222),
                                    fontSize: 28.sp,
                                  ),
                                ),
                              ],
                            ),
                            SizedBox(
                              height: 20.w,
                            ),
                            Offstage(
                              offstage: false,
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.center,
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    "积分优惠",
                                    style: TextStyle(
                                      color: Color(0xff666666),
                                      fontSize: 28.sp,
                                    ),
                                  ),
                                  Text(
                                    state.total["integral_discount"]
                                            .toString() ??
                                        '0',
                                    style: TextStyle(
                                      color: Color(0xff222222),
                                      fontSize: 28.sp,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Offstage(
                              offstage: false,
                              child: SizedBox(
                                height: 20.w,
                              ),
                            ),
                            state.total["freight_fee"] < 999
                                ? Row(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Text(
                                        "配送运费",
                                        style: TextStyle(
                                          color: Color(0xff666666),
                                          fontSize: 28.sp,
                                        ),
                                      ),
                                      Text(
                                        state.total["freight_fee"].toString(),
                                        style: TextStyle(
                                          color: Color(0xff222222),
                                          fontSize: 28.sp,
                                        ),
                                      ),
                                    ],
                                  )
                                : Container(),
                          ],
                        ),
                      ),
                      (state.isStop == false ||
                              state.total["freight_fee"] >= 999)
                          ? Container(
                              child: Text(
                                  '亲，此商品中燃合作的快递公司无法派送到您的地址,暂时无法下单，给您带来的不便，敬请谅解'),
                            )
                          : Container(),
                      Container(
                        height: 24.w,
                      ),
                    ],
                  )
                ]),
              ),
              state.isStop == true && state.total["freight_fee"] < 999
                  ? Container(
                      color: Colors.white,
                      width: 750.w,
                      height: 100.w,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              SizedBox(
                                width: 10.w,
                              ),
                              Text(
                                '￥${state.iscou == true ? state.total["all_payment"].toString() : state.total["all_payment"].toString()}',
                                style: TextStyle(
                                    color: Config.mainColor, fontSize: 32.w),
                              ),
                              SizedBox(
                                width: 20.w,
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    '优惠¥${state.iscou == true ? state.total["total_discount_fee"].toString() : state.total["total_discount_fee"].toString()}',
                                    style: TextStyle(
                                        color: Color(0xff999999),
                                        fontSize: 24.w),
                                  ),
                                  // if (state.type != 3 &&
                                  //     state.total["all_payment"].toString() !=
                                  //         '0.0')
                                  //   state.startMoney != null
                                  //       ? Text(
                                  //           '订金金额￥${state.startMoney}',
                                  //           style: TextStyle(
                                  //               color: Color(0xff999999),
                                  //               fontSize: 24.w),
                                  //         )
                                  //       : Text(
                                  //           '订金金额¥0.00',
                                  //           style: TextStyle(
                                  //               color: Color(0xff999999),
                                  //               fontSize: 24.w),
                                  //         ),
                                ],
                              ),
                            ],
                          ),
                          SizedBox(
                            width: 10.w,
                          ),
                          GestureDetector(
                            onTap: () {
                              if (state.number == 0 || state.disable) {
                                toast("当前商品均不在配送范围内，无法下单");
                                return;
                              }

                              eventBus.fire(InvoiceEvent(state.p));

                              if (state.total["freight_fee"] >= 999) {
                                toast('该地址不在配送范围内');
                                return;
                              }

                              if (state.isJiFen) {
                                logic.orderSubmit();
                                return;
                              } else {
                                if (state.total["all_payment"] <= 0) {
                                  logic.orderSubmit();
                                  return;
                                } else if (!subCorShopId(state.goodsItem)) {
                                  logic.orderSubmit();
                                  return;
                                } else if (state.GiftcardList.length <= 0) {
                                  logic.orderSubmit();
                                  return;
                                }
                                //
                                // print('提交订单');
                                // print(state.isJiFen);
                                // print(state.total["all_payment"]);
                                // print(state.GiftcardList.length);

                                setState(() {
                                  state.GiftcardList.forEach((element) {
                                    element['click'] = false;
                                  });
                                  state.isGiftNum = 0;
                                });
                                state.isGiftMoney = state.total["all_payment"];
                                state.realMoney = state.total["all_payment"];
                                showModalBottomSheet(
                                    context: context,
                                    // isDismissible: true,
                                    isScrollControlled: true,
                                    shape: RoundedRectangleBorder(
                                        borderRadius: BorderRadius.only(
                                            topLeft: Radius.circular(15),
                                            topRight: Radius.circular(15))),
                                    builder: (BuildContext context) {
                                      return StatefulBuilder(
                                          builder: (context1, state1) {
                                        return AnimatedPadding(
                                            //showModalBottomSheet 键盘弹出时自适应
                                            padding: MediaQuery.of(context)
                                                .viewInsets,
                                            duration: const Duration(
                                                milliseconds: 100),
                                            child: Container(
                                                constraints: BoxConstraints(
                                                  minHeight: 90,
                                                  maxHeight:
                                                      MediaQuery.of(context)
                                                              .size
                                                              .height /
                                                          1.2,
                                                ),
                                                child: ListView(
                                                    shrinkWrap: true,
                                                    children: [
                                                      Container(
                                                          alignment: Alignment
                                                              .bottomCenter,
                                                          padding:
                                                              EdgeInsets.only(
                                                                  top: 12),
                                                          child: Row(
                                                            children: [
                                                              SizedBox(
                                                                  width: 36),
                                                              Expanded(
                                                                child: Text(
                                                                    '选择支付方式',
                                                                    style: TextStyle(
                                                                        fontSize: 32
                                                                            .sp,
                                                                        color: Color(
                                                                            0xff666666),
                                                                        fontWeight:
                                                                            FontWeight
                                                                                .normal),
                                                                    textAlign:
                                                                        TextAlign
                                                                            .center),
                                                              ),
                                                              EsIconButton(
                                                                Icon(
                                                                  Icons.close,
                                                                  size: 24,
                                                                  color: Color(
                                                                      0xffcccccc),
                                                                ),
                                                                padding: EdgeInsets
                                                                    .only(
                                                                        right:
                                                                            12),
                                                                onTap: () {
                                                                  Navigator.of(
                                                                          context)
                                                                      .pop();
                                                                },
                                                              )
                                                            ],
                                                          )),
                                                      Padding(
                                                        padding:
                                                            const EdgeInsets
                                                                    .symmetric(
                                                                horizontal: 12,
                                                                vertical: 20),
                                                        child: Row(
                                                          children: [
                                                            // Image.asset(
                                                            //     'assets/images/selectIcon1.png',
                                                            //     width: 16,
                                                            //     height: 16),
                                                            SizedBox(
                                                              width: 8,
                                                            ),
                                                            Text(
                                                              '福利卡（已选${state.isGiftNum}张，共${state.GiftcardList.length}张）',
                                                              style: TextStyle(
                                                                  color: Color(
                                                                      0xff333333),
                                                                  fontSize:
                                                                      32.sp,
                                                                  fontWeight:
                                                                      FontWeight
                                                                          .normal),
                                                            ),
                                                            // Expanded(
                                                            //     child: Text(
                                                            //   '￥100.00',
                                                            //   maxLines: 1,
                                                            //   overflow:
                                                            //       TextOverflow
                                                            //           .ellipsis,
                                                            //   textAlign:
                                                            //       TextAlign
                                                            //           .end,
                                                            //   style: TextStyle(
                                                            //       color: EsColors
                                                            //           .TitleSecondary,
                                                            //       fontSize:
                                                            //           EsSizes
                                                            //               .MD,
                                                            //       fontWeight:
                                                            //           EsFontWeights
                                                            //               .Medium),
                                                            // ))
                                                          ],
                                                        ),
                                                      ),
                                                      Container(
                                                        margin: EdgeInsets.only(
                                                            left: 36,
                                                            bottom: 18),
                                                        height: 155,
                                                        child: ListView.builder(
                                                            itemCount: state
                                                                .GiftcardList
                                                                .length,
                                                            scrollDirection:
                                                                Axis.horizontal,
                                                            shrinkWrap: true,
                                                            itemBuilder:
                                                                (context,
                                                                    index) {
                                                              return GestureDetector(
                                                                onTap: () {
                                                                  state1(() {
                                                                    setGift(
                                                                        index);
                                                                  });

                                                                  print(
                                                                      '选择礼品卡');
                                                                },
                                                                child: Stack(
                                                                    children: [
                                                                      Container(
                                                                        width:
                                                                            190,
                                                                        padding:
                                                                            EdgeInsets.all(6),
                                                                        margin: EdgeInsets.only(
                                                                            right:
                                                                                10),
                                                                        child:
                                                                            Column(
                                                                          crossAxisAlignment:
                                                                              CrossAxisAlignment.start,
                                                                          mainAxisAlignment:
                                                                              MainAxisAlignment.start,
                                                                          children: [
                                                                            Image.asset('assets/images/welfare_card_cover.jpeg',
                                                                                width: 176,
                                                                                height: 100),
                                                                            // CachedImage(
                                                                            //     width: 176,
                                                                            //     height: 100,
                                                                            //     padding: EdgeInsets.zero,
                                                                            //     scale: BoxFit.fitWidth,
                                                                            //     path:
                                                                            //         'assets/images/welfare_card_cover'),
                                                                            Expanded(child: SizedBox(height: 2)),
                                                                            Text(
                                                                              '中秋节福利卡',
                                                                              style: TextStyle(color: EsColors.TitleSecondary, fontSize: EsSizes.XS, fontWeight: EsFontWeights.Normal),
                                                                            ),
                                                                            Row(
                                                                              children: [
                                                                                Expanded(
                                                                                  child: Text(
                                                                                    '￥${state.GiftcardList[index]['money_available'].toString()}',
                                                                                    maxLines: 1,
                                                                                    overflow: TextOverflow.ellipsis,
                                                                                    style: TextStyle(color: EsColors.TitleSecondary, fontSize: EsSizes.XS, fontWeight: EsFontWeights.Medium),
                                                                                  ),
                                                                                ),
                                                                                // Text(
                                                                                //   '有效期至:${ state.GiftcardList[index]['expire_dtme'].toString()}',
                                                                                //   style: TextStyle(
                                                                                //       color: EsColors.Fill,
                                                                                //       fontSize: EsSizes.XXS,
                                                                                //       fontWeight:
                                                                                //           EsFontWeights.Normal),
                                                                                // )
                                                                              ],
                                                                            )
                                                                          ],
                                                                        ),
                                                                        decoration: BoxDecoration(
                                                                            color: EsColors.White,
                                                                            border: Border.all(
                                                                              color: EsColors.Primary,
                                                                              width: 1,
                                                                            ),
                                                                            borderRadius: BorderRadius.circular(3)),
                                                                      ),
                                                                      if (state.GiftcardList[
                                                                              index]
                                                                          [
                                                                          'click'])
                                                                        Positioned(
                                                                            left:
                                                                                0,
                                                                            top:
                                                                                0,
                                                                            child: Image.asset('assets/images/selected_left.png',
                                                                                width: 47,
                                                                                height: 47))
                                                                    ]),
                                                              );
                                                            }),
                                                      ),
                                                      YpLine(),
                                                      GestureDetector(
                                                        behavior:
                                                            HitTestBehavior
                                                                .opaque,
                                                        onTap: () {
                                                          state1(() {
                                                            // index = 0;
                                                            print('切换支付方式微信');
                                                          });
                                                        },
                                                        child: Padding(
                                                          padding:
                                                              const EdgeInsets
                                                                      .symmetric(
                                                                  horizontal:
                                                                      12,
                                                                  vertical: 20),
                                                          child: Row(
                                                            children: [
                                                              Image.asset(
                                                                  'assets/images/selectIcon1.png',
                                                                  width: 16,
                                                                  height: 16),
                                                              SizedBox(
                                                                width: 8,
                                                              ),
                                                              Text(
                                                                '微信支付',
                                                                style: TextStyle(
                                                                    color: EsColors
                                                                        .TitleSecondary,
                                                                    fontSize:
                                                                        EsSizes
                                                                            .MD,
                                                                    fontWeight:
                                                                        EsFontWeights
                                                                            .Normal),
                                                              ),
                                                              Expanded(
                                                                  child: Text(
                                                                '￥${state.isGiftMoney}',
                                                                maxLines: 1,
                                                                overflow:
                                                                    TextOverflow
                                                                        .ellipsis,
                                                                textAlign:
                                                                    TextAlign
                                                                        .end,
                                                                style: TextStyle(
                                                                    color: EsColors
                                                                        .TitleSecondary,
                                                                    fontSize:
                                                                        EsSizes
                                                                            .MD,
                                                                    fontWeight:
                                                                        EsFontWeights
                                                                            .Medium),
                                                              ))
                                                            ],
                                                          ),
                                                        ),
                                                      ),
                                                      YpLine(),
                                                      // GestureDetector(
                                                      //   behavior:
                                                      //       HitTestBehavior
                                                      //           .opaque,
                                                      //   onTap: () {
                                                      //     state1(() {
                                                      //       // index = 1;
                                                      //       print(
                                                      //           '切换支付方式支付宝');
                                                      //     });
                                                      //   },
                                                      //   child: Padding(
                                                      //     padding:
                                                      //         const EdgeInsets
                                                      //                 .symmetric(
                                                      //             horizontal:
                                                      //                 12,
                                                      //             vertical:
                                                      //                 20),
                                                      //   ),
                                                      // ),
                                                      // YpLine(),
                                                      SizedBox(height: 27),
                                                      // Center(
                                                      //   child: RichText(
                                                      //     text: TextSpan(
                                                      //       children: [
                                                      //         TextSpan(
                                                      //           text: '应付：',
                                                      //           style: TextStyle(
                                                      //               color: EsColors.Primary,
                                                      //               fontSize: EsSizes.XS,
                                                      //               fontWeight: EsFontWeights.Normal),
                                                      //         ),
                                                      //         TextSpan(
                                                      //           text: '￥422.08',
                                                      //           style: TextStyle(
                                                      //               color: EsColors.Primary,
                                                      //               fontSize: EsSizes.LG,
                                                      //               fontWeight: EsFontWeights.Medium),
                                                      //         )
                                                      //       ],
                                                      //     ),
                                                      //   ),
                                                      // ),
                                                      SizedBox(height: 16),
                                                      EsBottomWidget(
                                                          leftTitle: null,
                                                          rightTitle: '确认付款',
                                                          rightlick: () {
                                                            print('确认付款');
                                                            Navigator.of(
                                                                    context)
                                                                .pop();

                                                            logic.orderSubmit();
                                                          },
                                                          decoration: null)
                                                    ])));
                                      });
                                    });
                              }
                            },
                            child: Container(
                              color: state.number > 0 && !state.disable
                                  ? Config.mainColor
                                  : Colors.grey,
                              height: 100.w,
                              width: 340.w,
                              child: Center(
                                child: Text(
                                  "提交订单",
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 36.sp,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    )
                  : Container(),
            ],
          )
        : Container();
  }

  // 在配送范围商品
  _inDeliveryView(BuildContext context, int index) {
    var itemViewList = [];
    var itemGiftList = [];
    var currentShopQuantity = 0;
    var currentGiftQuantity = 0;
    if (logic.state.goodsItem[index]["ec_packages"] != null) {
      itemViewList = logic.state.goodsItem[index]["ec_packages"];
    }

    itemViewList.forEach((item) {
      if (item['cartitemlist'] != null) {
        item['cartitemlist'].forEach((itm) {
          int inDeliveryNumber = itm['quantity'].toInt();
          currentShopQuantity += inDeliveryNumber;
        });
      }
    });

    // 礼物？
    if (logic.state.goodsItem[index]["cart_item_gifts"] != null) {
      itemGiftList = logic.state.goodsItem[index]["cart_item_gifts"];

      itemGiftList.forEach((element) {
        if (element['quantity'] != null) {
          currentGiftQuantity += element['quantity'].toInt();
        }
      });
    }

    if (itemViewList.length == 0 && itemGiftList.length == 0) {
      return Container();
    }

    return Visibility(
      visible: (itemViewList.length > 0),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        child: Row(
          children: [
            Container(
              height: 160.w,
              width: 600.w,
              padding: EdgeInsets.symmetric(vertical: 34.w),
              child: ListView(
                //设置水平方向排列
                scrollDirection: Axis.horizontal,
                //添加子元素
                children: (itemViewList[0]["cartitemlist"] + itemGiftList)
                    .map<Widget>((item) {
                  return Container(
                      margin: EdgeInsets.only(right: 10.w),
                      width: 96.w,
                      height: 96.w,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(
                          Radius.circular(10.w),
                        ),
                        border: Border.all(
                          color: Color(0xfff0f0f0),
                        ),
                      ),
                      child: CachedImage(
                        path: item["image_default_id"],
                        width: 160.w,
                        height: 160.w,
                      ));
                }).toList(),
              ),
            ),
            Expanded(
                child: Center(
              child: GestureDetector(
                onTap: () {
                  Get.toNamed(RouteConfig.orderGoodlist,
                      arguments:
                          (itemViewList[0]["cartitemlist"] + itemGiftList));
                },
                child: RichText(
                  text: TextSpan(
                    children: [
                      TextSpan(
                        text:
                            // "共${getproNum(itemViewList[0]["cartitemlist"] + itemGiftList)}件",
                            "共${(currentShopQuantity + currentGiftQuantity)}件",
                        style:
                            TextStyle(color: Color(0xffAAAAAA), fontSize: 26.w),
                      ),
                      WidgetSpan(
                          child: SizedBox(
                        width: 10.w,
                      )),
                      WidgetSpan(
                        child: Icon(
                          Icons.arrow_forward_ios_rounded,
                          size: 15,
                          color: Color(0xffB7B7B7),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            )),
          ],
        ),
      ),
    );
  }

  // 不在配送范围商品-提示
  _notInDeliveryTipView(BuildContext context, int index) {
    var itemViewList = [];
    if (logic.state.goodsItem[index]["stock_out_list"] != null) {
      itemViewList = logic.state.goodsItem[index]["stock_out_list"];
    }

    return Visibility(
      visible: (itemViewList.length > 0),
      child: Text(
        "以下商品在所选地区暂时无货，非常抱歉！",
        style: TextStyle(
          fontSize: 26.sp,
          color: Colors.red,
        ),
        textAlign: TextAlign.left,
      ),
    );
  }

  // 不在配送范围商品
  _notInDeliveryView(BuildContext context, int index) {
    var itemViewList = [];
    var allQuantity = 0;
    if (logic.state.goodsItem[index]["stock_out_list"] != null) {
      itemViewList = logic.state.goodsItem[index]["stock_out_list"];
    }

    itemViewList.forEach((item) {
      int notInDeliveryNumber = item['quantity'].toInt();
      allQuantity += notInDeliveryNumber;
    });

    return Visibility(
      visible: (itemViewList.length > 0),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        child: Row(
          children: [
            Container(
              height: 160.w,
              width: 600.w,
              padding: EdgeInsets.symmetric(vertical: 34.w),
              child: ListView(
                //设置水平方向排列
                scrollDirection: Axis.horizontal,
                //添加子元素
                children: itemViewList.map<Widget>((item) {
                  return Container(
                      margin: EdgeInsets.only(right: 10.w),
                      width: 96.w,
                      height: 96.w,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(
                          Radius.circular(10.w),
                        ),
                        border: Border.all(
                          color: Color(0xfff0f0f0),
                        ),
                      ),
                      child: CachedImage(
                        path: item["image_default_id"],
                        width: 160.w,
                        height: 160.w,
                      ));
                }).toList(),
              ),
            ),
            Expanded(
                child: Center(
              child: GestureDetector(
                onTap: () {
                  Get.toNamed(RouteConfig.orderGoodlist,
                      arguments: itemViewList);
                },
                child: RichText(
                  text: TextSpan(
                    children: [
                      TextSpan(
                        // "共${logic.state.goodsItem[0]['stock_out_list'].length}件",
                        text: "共${allQuantity}件",
                        style:
                            TextStyle(color: Color(0xffAAAAAA), fontSize: 26.w),
                      ),
                      WidgetSpan(
                          child: SizedBox(
                        width: 10.w,
                      )),
                      WidgetSpan(
                        child: Icon(
                          Icons.arrow_forward_ios_rounded,
                          size: 15,
                          color: Color(0xffB7B7B7),
                        ),
                      )
                    ],
                  ),
                ),
              ),
            )),
          ],
        ),
      ),
    );
  }

  /// 配送类型
  _showCupertinoActionSheet1(context) async {
    var result = await showCupertinoModalPopup(
        context: context,
        builder: (context) {
          return CupertinoActionSheet(
            title: Text('选择配送类型'),
            actions: <Widget>[
              CupertinoActionSheetAction(
                child: Container(
                  width: 500.w,
                  child: Stack(
                    children: [
                      Center(
                        child: Text('立即发货'),
                      ),
                      logic.state.deliveryType == 0
                          ? Positioned(
                              right: 0.w,
                              child: Icon(
                                Icons.check,
                                color: Colors.blue,
                                size: 48.sp,
                              ),
                            )
                          : Container(),
                    ],
                  ),
                ),
                onPressed: () {
                  logic.state.deliveryType = 0;
                  logic.update();
                  Navigator.of(context).pop('0');
                },
                isDefaultAction: true,
              ),
              CupertinoActionSheetAction(
                child: Container(
                  width: 500.w,
                  child: Stack(
                    children: [
                      Center(
                        child: Text('暂不发货'),
                      ),
                      logic.state.deliveryType == 2
                          ? Positioned(
                              right: 0.w,
                              child: Icon(
                                Icons.check,
                                color: Colors.blue,
                                size: 48.sp,
                              ),
                            )
                          : Container(),
                    ],
                  ),
                ),
                onPressed: () {
                  logic.state.deliveryType = 2;
                  logic.update();
                  Navigator.of(context).pop('2');
                },
                isDefaultAction: true,
              ),
              CupertinoActionSheetAction(
                child: Container(
                  width: 500.w,
                  child: Stack(
                    children: [
                      Center(
                        child: Text('预约发货'),
                      ),
                      logic.state.deliveryType == 1
                          ? Positioned(
                              right: 0.w,
                              child: Icon(
                                Icons.check,
                                color: Colors.blue,
                                size: 48.sp,
                              ),
                            )
                          : Container(),
                    ],
                  ),
                ),
                onPressed: () {
                  logic.state.deliveryType = 1;
                  logic.update();
                  Navigator.of(context).pop('1');
                },
                isDefaultAction: true,
              ),
            ],
            cancelButton: CupertinoActionSheetAction(
              child: Text('取消'),
              onPressed: () {
                Navigator.of(context).pop('cancel');
              },
            ),
          );
        });
  }

  void selectProduct(BuildContext context) {
    //用于在底部打开弹框的效果
    showModalBottomSheet(
        isScrollControlled: true,
        backgroundColor: Colors.white,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(10.0.w),
        ),
        builder: (BuildContext context) {
          //构建弹框中的内容
          return Container(
              height: 1000.w,
              width: 750.w,
              // decoration: BoxDecoration(
              //   borderRadius: BorderRadius.only(topLeft: Radius.circular(30.w),topRight: Radius.circular(30.w),),
              // ),
              child: CouponItem());
        },
        context: context);
  }

  Widget zitibody() {
    return Container(
      height: 400.w,
      child: Container(
        padding: EdgeInsets.symmetric(
          horizontal: 20.w,
        ),
        height: 400.w, // Change as per your requirement
        width: 300.w, // Ch
        child: ListView.builder(
          primary: true,
          shrinkWrap: true,
          itemBuilder: (ctx, i) {
            var item = state.selfHelpAddressList[i];
            return GestureDetector(
              onTap: () {
                logic.selectDian(i, item);
              },
              child: Container(
                padding: EdgeInsets.only(bottom: 15.w),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    state.selected == i
                        ? Image(
                            image: AssetImage("assets/images/selectIcon1.png"),
                            width: 36.w,
                          )
                        : Image(
                            image: AssetImage("assets/images/save.png"),
                            width: 36.w,
                          ),
                    SizedBox(
                      width: 10.w,
                    ),
                    Expanded(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            item["sub_unit_name"],
                            style: TextStyle(
                              color: Color(0xff222222),
                              fontSize: 28.sp,
                            ),
                          ),
                          Text(
                            '${item["prv_name"]}${item["city_name"]}${item["city_area_name"]}${item["town_name"] ?? ''}${item["adr"]}',
                            style: TextStyle(
                              color: Color(0xff999999),
                              fontSize: 24.sp,
                            ),
                            maxLines: 2,
                            textAlign: TextAlign.start,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ],
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(top: 10.w, bottom: 10.w),
                      child: Divider(
                        color: Color(0xff000000),
                      ),
                    ),
                  ],
                ),
              ),
            );
          },
          itemCount: state.selfHelpAddressList.length > 0
              ? state.selfHelpAddressList.length
              : 0,
        ),
      ),
    );
  }

  /// 自提点
  _selectProduct(context) {
    //打开弹框的效果
    return SimpleDialog(
      title: Text(
        '选择自提点',
        style: TextStyle(
          color: Color(0xff333333),
          fontSize: 26.w,
        ),
        textAlign: TextAlign.center,
      ),
      titleTextStyle: TextStyle(
          fontSize: 16, fontWeight: FontWeight.bold, color: Colors.blue),
      children: [
        GetBuilders(zitibody),
        SimpleDialogOption(
          onPressed: () {
            Navigator.pop(context);
            logic.submitDian();
          },
          child: Container(
            color: Colors.white,
            child: Container(
              height: 80.w,
              decoration: BoxDecoration(
                color: Config.mainColor,
                borderRadius: BorderRadius.circular(40.w),
              ),
              child: Center(
                child: Text(
                  "确定",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.w,
                  ),
                ),
              ),
            ),
          ),
        ),
      ],
      backgroundColor: Colors.white,
      elevation: 10,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(5),
      ),
    );
  }
}

///支付
///
///

//不同活动进入结算判断
Widget GetBuilders(wid) {
  //从购物车进入
  if (arguments == null) {
    return GetBuilder<OrderSettlementLogic>(builder: (logic) {
      return wid();
    });
  } else {
    return GetBuilder<OrderSettlementLogics>(builder: (logic) {
      return wid();
    });
  }
}

getagm() {
  print('Get.arguments');
  print(arguments);
  //从购物车进入
  if (arguments == null) {
    logic = Get.put(OrderSettlementLogic());
    state = Get.find<OrderSettlementLogic>().state;
  } else {
    logic = Get.put(OrderSettlementLogics());
    state = Get.find<OrderSettlementLogics>().state;
    //从购物车进入
    print('单品详情进入');
  }
}

showPayWay(BuildContext context, ClickCallback clickCallback, state, loice) {
  int index = 0;
}

class CouponItem extends StatefulWidget {
  CouponItem({Key key}) : super(key: key);

  @override
  _CouponItemState createState() {
    return _CouponItemState();
  }
}

class _CouponItemState extends State<CouponItem>
    with SingleTickerProviderStateMixin {
  TabController tabControllers;

  @override
  void initState() {
    tabControllers = TabController(length: 2, vsync: this);
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    // Widget availableRoll() {
    //   return
    // }
    return Column(children: [
      TabBar(controller: tabControllers, labelColor: Colors.black, tabs: [
        Tab(text: '可用优惠券'),
        Tab(text: '不可用优惠券'),
      ]),
      Expanded(
        child: TabBarView(
            controller: tabControllers,
            children: [availableRoll(), noavailableRollState()]),
      )
    ]);
  }
}

class availableRoll extends StatefulWidget {
  availableRoll({Key key}) : super(key: key);

  @override
  availableRollState createState() => availableRollState();
}

class availableRollState extends State<availableRoll> {
  @override
  TextEditingController _editingController = new TextEditingController();
  String editText = '';

  @override
  void initState() {
    super.initState();

    _editingController.addListener(() {
      ///获取输入的内容
      setState(() {
        editText = _editingController.text;
      });
      print(" controller 兼听 ${editText}");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 20.w),
            margin: EdgeInsets.only(right: 22.w, left: 22.w),
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border(
                bottom: BorderSide(
                  color: Color(0xfff0f0f0),
                ),
              ),
            ),
            child: SingleChildScrollView(child: GetBuilders(MainbodyAvailab)),
          ),
        )

        // GestureDetector(
        //     onTap: () async {
        //       logic.submitCoupon();
        //       Navigator.pop(context);
        //     },
        //     child: Padding(
        //       padding: EdgeInsets.all(20.w),
        //       child: ButtonWidget(title: "确定"),
        //     )),
      ],
    );
  }

  Widget MainbodyAvailab() {
    print('选着优惠券');
    print(state.couponList);
    print(state);
    return Column(
      children: [
        Container(
            width: double.infinity,
            height: 100.w,
            child: Row(
              children: [
                Container(
                  decoration: new BoxDecoration(
                    color: Colors.white,
                    //设置四周圆角 角度
                    borderRadius: BorderRadius.all(Radius.circular(4.0)),
                    //设置四周边框
                    border: new Border.all(width: 1, color: Colors.grey[200]),
                  ),
                  width: 540.w,
                  height: 80.w,
                  child: TextField(
                      controller: _editingController,
                      decoration: InputDecoration(
                        prefixIcon: GestureDetector(
                          onTap: () async {
                            var result = await BarcodeScanner.scan();
                            _editingController.text = result.rawContent;
                            // toast(result.rawContent);
                          },
                          child: Icon(Icons.qr_code_scanner),
                        ),
                      )),
                ),
                SizedBox(
                  width: 10.w,
                ),
                GestureDetector(
                  onTap: () {
                    if (editText == null || editText.length > 0) {
                      var params = {
                        'qbarcode': editText,
                        'access_token': SpUtil.getString('token'),
                        'usr_num_id': SpUtil.getInt('empeNumId'),
                      };
                      Api.infosync(params).listen((event) {
                        if (event['code'] == 0) {
                          toast('领取成功');
                        } else {
                          toast(event['message']);
                        }
                      });
                    }
                  },
                  child: Container(
                    decoration: new BoxDecoration(
                      color: editText == null || editText.length > 0
                          ? Colors.deepOrangeAccent
                          : Colors.grey[300],
                      //设置四周圆角 角度
                      borderRadius: BorderRadius.all(Radius.circular(4.0)),
                      //设置四周边框
                      border: new Border.all(width: 1, color: Colors.grey[200]),
                    ),
                    width: 120.w,
                    height: 80.w,
                    child: Center(
                        child:
                            Text('兑换', style: TextStyle(color: Colors.white))),
                  ),
                )
              ],
            )),
        ListView.builder(
          itemBuilder: (ctx, index) {
            var item = state.couponList[index];
            return InkWell(
              onTap: () {
                Navigator.pop(context);
                logic.selectCoupon(index, item);
              },
              child: Container(
                width: 718.w,
                height: 208.w,
                margin: EdgeInsets.symmetric(vertical: 10.w),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(
                    Radius.circular(16.w),
                  ),
                  image: DecorationImage(
                    image: AssetImage("assets/images/couponbg.png"),
                    fit: BoxFit.cover,
                  ),
                  boxShadow: [
                    BoxShadow(
                      color: Color(0xFF6498F1).withOpacity(.14),
                      offset: Offset(0, 0), //阴影y轴偏移量
                      blurRadius: 1,
                      spreadRadius: .3,
                    )
                  ],
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      width: 200.w,
                      padding: EdgeInsets.only(top: 50.w),
                      child: Column(
                        children: [
                          RichText(
                            text: TextSpan(
                              children: [
                                TextSpan(
                                  text: item['qtype'] == '0' ? '' : '￥',
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 28.w,
                                  ),
                                ),
                                TextSpan(
                                  text: item['qtype'] == '0'
                                      ? (item['qmz'] / 10).toStringAsFixed(0)
                                      : item['qmz'].toString(),
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 50.w,
                                  ),
                                ),
                                TextSpan(
                                  text: item['qtype'] == '0' ? '折' : '',
                                  style: TextStyle(
                                    color: Color(0xffC99F4F),
                                    fontSize: 50.w,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          Text(
                            "满${item['least_cost'].toString()}元可用",
                            style: TextStyle(
                              color: Color(0xffC99F4F),
                              fontSize: 22.w,
                            ),
                          )
                        ],
                      ),
                    ),
                    SizedBox(width: 10.w),
                    Container(
                      width: 310.w,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Text(
                            item['qname'].toString(),
                            style: TextStyle(
                              color: Color(0xff000000),
                              fontSize: 28.sp,
                            ),
                            maxLines: 1,
                          ),
                          SizedBox(height: 60.w),
                          Text(
                            "${item['sdate'].split(" ")[0]}-${item['edate'].split(" ")[0]}",
                            style: TextStyle(
                              color: Color(0xff999999),
                              fontSize: 24.sp,
                            ),
                          ),
                          // SizedBox(height: 20.w),
                          // Text(
                          //   "剩余990张",
                          //   style: TextStyle(
                          //     color: Color(0xff666666),
                          //     fontSize: 24.sp,
                          //   ),
                          // ),
                        ],
                      ),
                    ),
                    SizedBox(width: 20.w),
                    Container(
                      padding: EdgeInsets.only(right: 10.w),
                      child: Image.asset(
                        item['show_status'] == 1
                            ? "assets/images/select2.png"
                            : "assets/images/select1.png",
                        width: 35.w,
                      ),
                    )
                  ],
                ),
              ),
            );
          },
          itemCount: state.couponList.length,
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
        )
      ],
    );
  }
}

class noavailableRollState extends StatefulWidget {
  noavailableRollState({Key key}) : super(key: key);

  @override
  _noavailableRollStateState createState() => _noavailableRollStateState();
}

class _noavailableRollStateState extends State<noavailableRollState> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 20.w),
            margin: EdgeInsets.only(right: 22.w, left: 22.w),
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border(
                bottom: BorderSide(
                  color: Color(0xfff0f0f0),
                ),
              ),
            ),
            child:
                SingleChildScrollView(child: GetBuilders(MainbodyAvailabRoll)),
          ),
        ),
      ],
    );
  }

  MainbodyAvailabRoll() {
    return ListView.builder(
      itemBuilder: (ctx, index) {
        var item = state.nocouponList[index];
        print(item);
        return Container(
          width: 718.w,
          height: 208.w,
          margin: EdgeInsets.symmetric(vertical: 10.w),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(
              Radius.circular(16.w),
            ),
            image: DecorationImage(
              image: AssetImage("assets/images/couponbg.png"),
              fit: BoxFit.cover,
            ),
            boxShadow: [
              BoxShadow(
                color: Color(0xFF6498F1).withOpacity(.14),
                offset: Offset(0, 0), //阴影y轴偏移量
                blurRadius: 1,
                spreadRadius: .3,
              )
            ],
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                width: 200.w,
                padding: EdgeInsets.only(top: 50.w),
                child: Column(
                  children: [
                    RichText(
                      text: TextSpan(
                        children: [
                          TextSpan(
                            text: item['qtype'] == '0' ? '' : '￥',
                            style: TextStyle(
                              color: Color(0xffC99F4F),
                              fontSize: 28.w,
                            ),
                          ),
                          TextSpan(
                            text: item['qtype'] == '0'
                                ? (item['qmz'] / 10).toStringAsFixed(0)
                                : item['qmz'].toString(),
                            style: TextStyle(
                              color: Color(0xffC99F4F),
                              fontSize: 50.w,
                            ),
                          ),
                          TextSpan(
                            text: item['qtype'] == '0' ? '折' : '',
                            style: TextStyle(
                              color: Color(0xffC99F4F),
                              fontSize: 50.w,
                            ),
                          ),
                        ],
                      ),
                    ),
                    Text(
                      "满${item['least_cost'].toString()}元可用",
                      style: TextStyle(
                        color: Color(0xffC99F4F),
                        fontSize: 22.w,
                      ),
                    )
                  ],
                ),
              ),
              SizedBox(width: 30.w),
              Container(
                width: 310.w,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      item['qname'].toString(),
                      style: TextStyle(
                        color: Color(0xff000000),
                        fontSize: 28.sp,
                      ),
                      maxLines: 1,
                    ),
                    SizedBox(height: 60.w),
                    Text(
                      "${item['sdate'].split(" ")[0]}-${item['edate'].split(" ")[0]}",
                      style: TextStyle(
                        color: Color(0xff999999),
                        fontSize: 24.sp,
                      ),
                    ),
                    // SizedBox(height: 20.w),
                    // Text(
                    //   "剩余990张",
                    //   style: TextStyle(
                    //     color: Color(0xff666666),
                    //     fontSize: 24.sp,
                    //   ),
                    // ),
                  ],
                ),
              ),
            ],
          ),
        );
      },
      itemCount: state.nocouponList.length,
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
    );
  }
}
